﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using tmAsk.Common;

namespace tmAsk.Member
{
    public partial class MyAsk : System.Web.UI.Page
    {

        tmAsk.Data.MakeHtml make = new tmAsk.Data.MakeHtml();
        tmAsk.Data.MakehtmlSQL SQL = new tmAsk.Data.MakehtmlSQL();

        protected void Page_Load(object sender, EventArgs e)
        {
            function.CheckUserLogin();
            string uid = function.Getcookies();
            int pageindex = 1;  //页码
            int page = 20;       //每页条数
            int pagecount = 1;  //总页数
            int count = 0;        //共多少条

            if (!string.IsNullOrEmpty(Request.QueryString["page"]))
            {
                pageindex = Convert.ToInt32(Request.QueryString["page"]);
            }

            DataRow dr = SQL.GetList("b_user", "u_id='" + uid + "'", "u_id").Tables[0].Rows[0];
            // string sql = "select * from (select top " + page + " * from (select top " + pageindex * page + " * from B_ask where u_id=" + dr["u_id"].ToString() + " and a_delete=0 order by a_id desc) order by a_id asc) order by a_id desc";
            DataSet ds = SQL.GetList("b_ask", "a_id", page, pageindex, "a_time desc", 1, "u_id=" + dr["u_id"].ToString());

            rptList.DataSource = ds;
            rptList.DataBind();

            DataSet ds2 = SQL.GetList("b_ask", "a_delete=0 and u_id=" + dr["u_id"].ToString() + "order by a_time desc", "a_id,a_time");
            
            if ((ds2.Tables[0].Rows.Count % page) > 0)
            {
                pagecount = ds2.Tables[0].Rows.Count / page + 1;
            }
            else
            {
                pagecount = ds2.Tables[0].Rows.Count / page;
            }

            count = ds2.Tables[0].Rows.Count;

            lblTxt.Text = make.htmlpage(count, page, pageindex, "?", "page=");
        }

        public string GetUrl(object obj)
        {
            return (new function()).Getlink("ask", obj.ToString());
        }

        public string GetTitle(object obj)
        {
            string askstatus = "normal";
            DataRow dr = SQL.GetList("select * from b_ask where a_id = " + obj.ToString() + "").Tables[0].Rows[0];
            int Ischeck = Convert.ToInt32(dr["a_ischeck"]);
            if (Ischeck == 1)
            {
                DataSet askrn = SQL.GetList("select * from b_review where a_id = " + obj.ToString() + "");
                if (askrn.Tables[0].Rows.Count > 0)
                {
                    askstatus = "nore";
                }
                else
                {
                    askstatus = "ischeck";
                }
            }
            return "<a title=\"" + dr["a_title"].ToString() + "\" class=\"" + askstatus + "\" href='editAsk.aspx?id=" + obj.ToString() + "' target='ucIframe'>" + CutString(dr["a_title"].ToString(), 32) + "</a>";
        }

        public string CutString(string str, int t)//截取字符长度
        {
            if (str.Length > t)
            {
                return str.Substring(0, t) + "…";
            }
            return str;
        }

        public string GetState(object obj)
        {
            if (obj.ToString() == "0")
            {
                return "<span style=\"color: #db1111;\">未解决</span>";
            }
            return "<span>已解决</span>";
        }

        public string GetReviewNumber(object obj,object aid)
        {
            if (obj.ToString() == "0")
            {
                return "<span style=\"color: #cccccc;\">0</span>";
            }
            return "<a title=\"查看\" href=\"/?ask-" + aid.ToString() + "\" target=\"_blank\">" + obj.ToString() + "</a>";
        }

        public string GetIscheck(object t, object aid)
        {
            if (t.ToString() == "1")
            {
                return "<span style=\"color: #db1111;\">未审核</span>";
            }
            else
            {
                return "<a title=\"查看\" href=\"/?ask-" + aid.ToString() + "\" target=\"_blank\">已审核</a>";
            }
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            string str = Request.Form["AskDel"];
            if (str == null || str == "")
            {
                Response.Write(function.alert_text(4, "尚未选择数据", ""));
                return;
            }

            string[] id = str.Split(',');
            foreach (string item in id)
            {
                string sql = "update b_ask set a_delete=1 where a_id=" + item;
                SQL.ExecuteSql(sql);
            }
            Response.Write(function.alert_text(2, "申请删除成功", "myask.aspx"));
        }
    }
}